﻿using System;
using System.Collections.ObjectModel;

namespace Pixster.Slideshow.BusinessObjects
{
    /// <summary>
    /// Represents a slide show.
    /// </summary>
    public class PhotoSlideShow
    {
        private int _currentPhotoIndex;
        private readonly Random _random = new Random();

        public PhotoSlideShow(ObservableCollection<Photo> photos)
        {
            this.Photos = photos;
            if (photos != null)
            {
                MoveNext();
            }
        }

        public ObservableCollection<Photo> Photos { get; protected set; }

        /// <summary>
        /// Gets the previous photo in the slide show.
        /// </summary>
        public Photo PreviousPhoto
        {
            get
            {
                if (this.Photos == null)
                    return null;

                var prevPhotoIndex = this._currentPhotoIndex - 1;
                if (this._currentPhotoIndex == 0)
                    prevPhotoIndex = this.Photos.Count - 1;
                return this.Photos[prevPhotoIndex];
            }
        }

        /// <summary>
        /// Gets the current photo in the slide show.
        /// </summary>
        public Photo CurrentPhoto
        {
            get
            {
                return this.Photos == null ? null : this.Photos[this._currentPhotoIndex];
            }
        }

        /// <summary>
        /// Gets the next photo in the slide show.
        /// </summary>
        public Photo NextPhoto
        {
            get
            {
                return this.Photos == null ? null : this.Photos[this._currentPhotoIndex];
            }
        }

        public void MoveNext()
        {
            this._currentPhotoIndex = _random.Next(this.Photos.Count);
        }

        public void MovePrevious()
        {
            this._currentPhotoIndex = this._currentPhotoIndex == 0
                                          ? this.Photos.Count - 1
                                          : this._currentPhotoIndex - 1;
        }
    }
}